package communication.definitions;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * The <code>HeaderGenerator</code> is reads the associated integer values of
 * each enumeration of the enumerated types, <code>ReceivedFunction</code>,
 * <code>TransmitableFunction</code>, and <code>DeviceType</code>, and prints
 * out or writes to a file the header file necessary for the robot and client to
 * communicate.
 * 
 * @author Peter Kalauskas
 */
public class HeaderGenerator {
	public static void main(String[] args) throws IOException {
		String s = getHeaderPrintout();

		// The location of device.h. Uncomment this line to write to the file:

		// writeToFile("../Windex Robot/modules/device.h",s);

		System.out.println(s);
	}

	/**
	 * Writes a string to a file given by a directory represented as a string.
	 * 
	 * @param filename
	 *            the path of the file to be written
	 * @param s
	 *            the string to write to the file
	 * @throws IOException
	 */
	public static void writeToFile(String filename, String s)
			throws IOException {
		File f = new File(filename);
		f.createNewFile();
		BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
				filename));
		// Start writing to the output stream
		bufferedWriter.write(s);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		bufferedWriter.close();
	}

	/**
	 * Returns a string of what should be inside the header file device.h.
	 * 
	 * @return a string representing what should be inside the header file
	 *         device.h
	 */
	public static String getHeaderPrintout() {
		String s = "";
		s += "/**\n"
				+ " * @brief Device type and function definitions for transmitting and receiving commands.\n"
				+ " * @file device.c\n" + " *\n"
				+ " * Header file generated by HeaderGenerator.java.\n"
				+ " *\n" + " * @author Peter Kalauskas\n */\n";

		s += "#ifndef DEVICE_H_\n#define DEVICE_H_\n\n";

		s += "// Device types defined in DeviceType.java\n"
				+ "// These are used along with the function identifiers below to\n"
				+ "// locate a specific function to call.\n";
		for (DeviceType dt : DeviceType.values()) {
			s += "#define " + dt + " " + dt.ordinal() + "\n";
		}
		s += "\n";

		s += "// Functions defined in ReceivedFunction.java\n"
				+ "// These are functions which the robot can send to the client.\n";
		for (ReceivedFunction rf : ReceivedFunction.values()) {
			s += "#define " + rf + " " + rf.ordinal() + "\n";
		}
		s += "\n";

		s += "// Functions defined in TransmitableFunction.java\n"
				+ "// These are functions which the robot will need to be able to receive.\n"
				+ "// To receive a command a function must be registered using the\n"
				+ "// register_action function in dispatcher.c\n";
		for (TransmitableFunction tf : TransmitableFunction.values()) {
			s += "#define " + tf + " " + tf.ordinal() + "\n";
		}
		s += "\n";

		s += "#endif /* DEVICE_H_ */\n";
		return s;
	}
}
